#!/usr/bin/env python
'''Gui

Some useful classes for GUIs shortcuts
'''

import gtk
import gobject

from Countries import Country

class ErrorBox(gtk.MessageDialog):
    '''A convenient class for error boxes'''

    def __init__(self, parent=None, str="Error!"):
        gtk.MessageDialog.__init__(self, parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, str)
        self.set_title("Error!")
        if self.run():
            self.destroy()

class InfoBox(gtk.MessageDialog):
    '''A convenient class for info boxes'''

    def __init__(self, parent=None, str="Informations..."):
        gtk.MessageDialog.__init__(self, parent, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT
                , gtk.MESSAGE_INFO, gtk.BUTTONS_OK, str)
        self.set_title("Informations...")
        if self.run():
            self.destroy()

def setImageFilter():
    '''Builds a filter for image types to use with filechooser'''
    filter = gtk.FileFilter()
    filter.set_name("Images")
    filter.add_mime_type("image/png")
    filter.add_mime_type("image/jpeg")
    filter.add_mime_type("image/gif")
    filter.add_pattern("*.png")
    filter.add_pattern("*.jpg")
    filter.add_pattern("*.gif")
    return filter

def setCombobox(combo, datas, active=None, has_na=True):
    if (not isinstance(combo, gtk.ComboBox)):
        raise TypeError, "combo must be gtk.ComboBox() instance"

    model = gtk.ListStore(gobject.TYPE_STRING)
    combo.set_model(model)

    if (has_na == True):
        combo.append_text("N/A")
    else:
        combo.append_text("")
    combo.set_active(0)

    i = 0
    for x in datas:
        i = i + 1
        combo.append_text(x)
        if (x == active):
            combo.set_active(i)

def setComboboxCountries(combo, active=None, order="asc"):
    '''Set up the model for a combobox containing the list of countries'''
    if (not isinstance(combo, gtk.ComboBox)):
        raise TypeError, "combo must be gtk.ComboBox() instance"
    order = order.lower()
    if ((order != "asc") and (order != "desc")):
        raise AttributeError, "order must be 'asc' or 'desc'"

    setCombobox(combo, Country(order), active)


def treeviewAddGeneralTextColumn(treeview, name, pos = 0, resizable=True, reorderable=False):
    '''Add a new text column to the model'''
    cell = gtk.CellRendererText()
    col = gtk.TreeViewColumn(name, cell, text = pos)
    col.set_resizable(resizable)
    col.set_reorderable(reorderable)
    treeview.append_column(col)
    treeview.set_headers_clickable(True)

    if (reorderable):
        col.set_sort_column_id(pos)

    return cell, col

def treeviewAddGeneralPixbufColumn(treeview, name, pos = 0, resizable=True, reorderable=False):
    '''Add a new gtk.gdk.Pixbuf column to the model'''
    cell = gtk.CellRendererPixbuf()
    col = gtk.TreeViewColumn(name, cell, pixbuf = pos)
    col.set_resizable(resizable)
    col.set_reorderable(reorderable)
    treeview.append_column(col)
    treeview.set_headers_clickable(True)

    if (reorderable):
        col.set_sort_column_id(pos)

    return cell, col
